/**
 * Created by jt on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/jquery.min.js";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.multi-select.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";
import "../../../unit/common/js/bootstrap_js/table-editable.js";
import "../../../unit/common/js/bootstrap_js/form-components.js";

jQuery(document).ready(function() {
    App.init();
    // FormComponents.init();
    var token = sessionStorage.getItem('token');
    if (token == null || token =='') {
        location.href = HOST_FRT+'/logins/login.html';
    }
    jQuery('.logOut').attr('href','../logins/login.html');

    let picFlag = true;
    //新增附件
    jQuery('.addFile').click(function () {
        jQuery('#atchFile').click();

        $("#atchFile").unbind().change(function(){
            picFlag = false;
            let formData = new FormData();
            let atchNam=$("#atchFile")[0].files[0].name;
            let atchUrl;
            console.log(atchNam);
            formData.append('file', $("#atchFile")[0].files[0]);

            // console.log(formData);
            $.ajax({
                url: HOST_ADDR+'/cpmp/file/upload/atch',
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                success: function(resp){
                    if(resp.success){
                        // alert('上传文件成功');
                        console.log(resp);
                        atchUrl=resp.data;
                        picFlag = true;
                        jQuery('#tab_3').append('<div class="controls atchDiv"><span src="'+atchUrl+'">'+atchNam+'</span>' +
                            '&emsp;<a class="delete underline" href="javascript:;"><i class="icon-trash"></i> 删除</a></div>');
                        jQuery('.delete').click(function () {
                            jQuery(this).parent('div').remove();
                        });
                    }else{
                        alert(resp.msgInf);
                    }
                },
                error:function(err){
                    alert('上传文件失败！');
                }
            });
        });
    });

    // 保存   发布
    jQuery('.form-actions .btn.pushData').on('click',function(){
        let pubFlg;
        if($(this).hasClass('subKeep')){  // 保存
            pubFlg = '0';
        }else if($(this).hasClass('sharePro')){
            pubFlg = '1';                   // 发布
        }
        let artclTtl = jQuery('.artclTtl').val();
        let artclTypCod = jQuery('.artclTypCod').val();
        let artclCtt = editor.getValue();
        let atchDtos = [];
        let atchDiv = jQuery('.atchDiv');
        if (atchDiv.length > 0){
            for (var i= 0;i<atchDiv.length;i++) {
                atchDtos.push({atchNam:jQuery(atchDiv[i]).find('span').text(),atchUrl:jQuery(atchDiv[i]).find('span').attr('src')});
            }
        }
        if (artclTtl == '') {
            alert('请填写文章标题！');
            return;
        }
        if (artclTypCod == '') {
            alert('请选择文章类型！');
            return;
        }
        if (artclCtt == '') {
            alert('请填写文章内容！');
            return;
        }
        let data = {
            "pubFlg":pubFlg,
            "artclTtl":artclTtl,
            "artclTypCod": artclTypCod,
            "artclCtt": artclCtt,
            "atchDtos": atchDtos
        };
        console.log(data);
        jQuery.ajax({
            url: HOST_ADDR+'/cpmp/artcl/add',
            type: 'post',
            data: JSON.stringify(data),
            dataType: 'json',
            contentType:"application/json",
            headers: {
                'access_token': token,'login_type':'ums'
            },
            success: function(resp){
                if (!resp.success) {
                    alert(resp.msgCod+' '+resp.msgInf);
                    return;
                }
                console.log(resp);
                pubFlg==1 ? alert("发布成功！") : alert("保存成功！");
                location.href = 'article_list.html';

            },
            error:function(err){
                console.log(err);
            }
        });
    });
});